<?php


namespace apps\marriage\core\logic;


use vm\org\captcha\Captcha;

class CaptchaLogic
{
    const CACHE_PREFIX = 'image_captcha:';

    public function entry()
    {
        $sessionId = rand_string(32);
        $captcha = new Captcha();
        $captcha = $captcha->entry();
        cache(self::CACHE_PREFIX . $sessionId, $captcha['code'], 600);
        return [
            'session_id' => $sessionId,
            'captcha' => $captcha['image']
        ];
    }

    public function verify($sessionId, $inputCode)
    {
        $cachedCode = cache(self::CACHE_PREFIX . $sessionId);
        cache(self::CACHE_PREFIX . $sessionId, null);
        return strtoupper($cachedCode) == strtoupper($inputCode);
    }
}